/*
Author: Eric Pfeiffer (computerfr33k)
Desription: Program for keeping your league of legends preferences synced across multiple computers.

Copyright 200X Eric Pfeiffer

This file is part of LoL Preferences Sync.

LoL Preferences Sync is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Foobar is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Foobar. If not, see http://www.gnu.org/licenses/.
*/

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <curl/curl.h>
#include <stdio.h>

using namespace std;

// Main Menu for program
int MainMenu()
{
    int choice;
    cout << "--------- Main Menu ---------" << endl;
    cout << "1) Upload Preferences" << endl;
    cout << "2) Download Preferences" << endl;
    cout << "3) Exit" << endl;
    cout << "-----------------------------" << endl;
    cout << "Enter An Option: ";
    cin >> choice;
    return choice;
}

size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
    size_t written;
    written = fwrite(ptr, size, nmemb, stream);
    return written;
}

void curl_download()
{
    string summoner;
    string region;

    cin.get(); //get rid of newline char
    cout << "Summoner Name: ";
    //cin >> summoner;
    getline(cin, summoner);

    cout << endl << "Region(US, EU, EUW): ";
    cin >> region;

    if(region == "us" || region == "US")
    {
        region = "US";
    }
    else if(region == "eu" || region == "EU")
    {
        region = "EU";
    }
    else if(region == "euw" || region == "EUW")
    {
        region = "EUW";
    }
    else
    {
        cout << "Oops! Something Went Wrong" << endl;
        cout << "Valid Choices are US, EU, or EUW" << endl;
    }

    string summonerNew;

    // build URL for desired summoner name
    /* summonerNew = summoner;
    summonerNew.insert(0, "http://www.computerfreak.wastedmemory.com/lol-preferences/upload/");
    summonerNew.insert(65, region);
    summonerNew.append(".properties");
	*/
    summoner.append(".properties"); //add .properties extension to summoner name

	summonerNew = "http://www.computerfreak.wastedmemory.com/lol-preferences/upload/";
	summonerNew.append(region);
	summonerNew.append("/");
	summonerNew.append(summoner);

    // convert url string into a const char
    const char *p;
    p=summonerNew.c_str();

    // convert
    const char *a;
    a=summoner.c_str();

    CURL *curl;
    FILE *fp;
    CURLcode res;
    const char *url = p;
    //const char outfilename[FILENAME_MAX] = p;
    curl = curl_easy_init();
    if (curl)
    {
        fp = fopen(a,"wb"); //open file we want to download contents too.
        curl_easy_setopt(curl, CURLOPT_URL, url); //goto url where the summoner's preferences exist
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        fclose(fp);
    }
}

void Error()
{
    cout << "Oops! Something Went Wrong" << endl;
}

int main(int argc, char *argv[])
{
    int run = 0;
    while(true)
    {
        run++;
        if(run > 1)
        {
            cout << "\n\n\n\n\n\n\n";
        }
        int choice = MainMenu();

        if(choice == 1)
        {
            CURL *curl;
            CURLcode res;

            string summoner;
            string region;

            cin.get(); //get rid of newline char
            cout << "Summoner Name: ";
            //cin >> summoner;
            getline(cin, summoner);

            cout << endl << "Region(US, EU, EUW): ";
            cin >> region;

            if(region == "us" || region == "US")
            {
                region = "US";
            }
            else if(region == "eu" || region == "EU")
            {
                region = "EU";
            }
            else if(region == "euw" || region == "EUW")
            {
                region = "EUW";
            }
            else
            {
                cout << "Oops! Something Went Wrong" << endl;
                cout << "Valid Choices are US, EU, or EUW" << endl;
            }

            summoner.append(".properties");

            const char *p;
            p=summoner.c_str();

            const char *regionNew;
            regionNew=region.c_str();

            struct curl_httppost *formpost=NULL;
            struct curl_httppost *lastptr=NULL;
            struct curl_slist *headerlist=NULL;
            static const char buf[] = "Expect:";

            curl_global_init(CURL_GLOBAL_ALL);

            /* Fill in the file upload field */
            curl_formadd(&formpost,
                         &lastptr,
                         CURLFORM_COPYNAME, "file",
                         CURLFORM_FILE, p,
                         CURLFORM_END);

            /* Fill in the region field */
            curl_formadd(&formpost,
                         &lastptr,
                         CURLFORM_COPYNAME, "region",
                         CURLFORM_COPYCONTENTS, regionNew,
                         CURLFORM_END);

            /* Fill in the submit field too, even if this is rarely needed */
            curl_formadd(&formpost,
                         &lastptr,
                         CURLFORM_COPYNAME, "submit",
                         CURLFORM_COPYCONTENTS, "Upload",
                         CURLFORM_END);

            curl = curl_easy_init();
            /* initalize custom header list (stating that Expect: 100-continue is not
               wanted */
            headerlist = curl_slist_append(headerlist, buf);
            if(curl)
            {
                /* what URL that receives this POST */
                curl_easy_setopt(curl, CURLOPT_URL, "http://www.computerfreak.wastedmemory.com/lol-preferences/upload.php");
                if ( (argc == 2) && (!strcmp(argv[1], "noexpectheader")) )
                    /* only disable 100-continue header if explicitly requested */
                    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
                curl_easy_setopt(curl, CURLOPT_POST, true);
                curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
                res = curl_easy_perform(curl);

                /* always cleanup */
                curl_easy_cleanup(curl);

                /* then cleanup the formpost chain */
                curl_formfree(formpost);
                /* free slist */
                curl_slist_free_all (headerlist);
            }
        }
        else if(choice == 2)
        {
            curl_download();
        }
        else if(choice == 3) //Exit
        {
            break;
        }
        else
        {
            Error();
            break;
        }
    }
    return 0;
}
